package com.geek.config.microprofile.source;

import javax.servlet.ServletContext;
import java.util.Enumeration;
import java.util.Map;

import static com.geek.config.enums.ConfigSourceEnum.SERVLET_CONTEXT_CONFIG;

/**
 * @Author: ysq
 * @Date: 2021/7/14
 * @Description:
 */
public class ServletContextConfigSource extends MapBaseConfigSource{
  private final ServletContext servletContext;

  public ServletContextConfigSource(final ServletContext ctx) {
    super(SERVLET_CONTEXT_CONFIG.getName(), SERVLET_CONTEXT_CONFIG.getOrdinal());
    this.servletContext = ctx;
  }

  @Override
  protected void prepareConfigData(final Map<String, String> configData) {
    final Enumeration<String> parameterNames = servletContext.getInitParameterNames();
    while (parameterNames.hasMoreElements()) {
      String parameterName =  parameterNames.nextElement();
      configData.put(parameterName, servletContext.getInitParameter(parameterName));
    }
  }
}
